var deleteFlag ;
var levelData = [];
var abnormalRuleData = [];
var abnormalLevelData = [];

$(document).ready(function() {
	showLoading();
	$('title', parent.document).html($('title').text());
	setBaseUrl();
	createTableHead("abnormalRuleThead", abnormalRuleTable);
	$ajaxList('abnormalLevel', getAbnormalLevel);
	setTimeout(search,500);
});
function getAbnormalLevel(data){
	var length = data.length;
	abnormalLevelData = data;
}
function search() {
	$ajax('searchAbnormalRule', '', searchAbnormalRule);
}
function searchAbnormalRule(data) {
	abnormalRuleData = data && data.abnormalRuleList || [];
	createTableBody("abnormalRuleTbody", abnormalRuleTable, abnormalRuleData, false, true);
	setTimeout(hideLoading, 500);
}
function chkBefore(index, chkFlag){
	var checkbox =document.getElementsByName("chk"); 
	for (var i=0; i<checkbox.length; i++) {
		checkbox[i].checked = false; 
	}
	checkbox[index].checked = true; 
	if (chkFlag == "update") {
		update(index);
	} else if(chkFlag == "delete") {
		del();
	}
}
function save() {
	deleteFlag = "save";
	addTitle("abnormalRuleSave");
	allTable();
}
function update(index) {
	$('#title').html('');
	deleteFlag = "update";
	addTitle("abnormalRuleUpdate");
	allTable(index);
}
function allTable(index){
	createDetailHead("detailThead", abnormalRuleTable, 1);
	createDetailBody("detailTbody", abnormalRuleTable, abnormalRuleData[index], 1);
	for (var i = 0;i < abnormalLevelData.length; i++){
        $("#abnormalLevel").append("<option value='" + abnormalLevelData[i].abnormalLevel + "'>" + abnormalLevelData[i].abnormalLevelContent + "</option>");
    }
	if(deleteFlag == "update") {
		$("#abnormalLevel").val(abnormalRuleData[index].abnormalLevel);
		$("#abnormalID").attr("readonly", true);
		$('#confirm').show();
		updateIndex = index;
	} else if(deleteFlag == "save") {
		$("#abnormalID").attr("readonly", false);
		$('#confirm').show();
	}
	$("#save").modal("show");
}
function confirm(){
	return checkForm("saveForm", abnormalRuleTable);
}
function getUrl(){
	if(deleteFlag == "save"){
		return 'saveAbnormalRule';
	} else if(deleteFlag == "update"){
		return 'updateAbnormalRule';
	} else if(deleteFlag == "delete"){
		return 'deleteAbnormalRule';
	}
}
function getData(){
	return {abnormalID: getCheckDataList(abnormalRuleData, "abnormalID")};
}
function saveAbnormalRule(data){
	if(deleteFlag == "save"){
	     var result = data && data.result;
	     if (result == "Success") {
	    	 showMessge(messageContent["Insert.Success"]);
	    	 $('#save').modal('hide');
	    	 formReset("saveForm");
	     } else if (result == "NotFound"){
	    	 showMessge(messageContent["Insert.Failed"]);
	     } else if (result == "AbnormalIDExist"){
	         showMessge(messageContent["AbnormalRule.AbnormalIDExist"]);
	     } else {
	    	 showMessge(messageContent["Insert.Error"]);
	     }
	     search();
	}else if(deleteFlag == "update"){
	     var result = data && data.result;
	     if (result == "Success"){
	    	 showMessge(messageContent["Update.Success"]);
	    	 $('#save').modal('hide');
	    	 formReset("saveForm");
	     } else if (result == "NotFound"){
	    	 showMessge(messageContent["Update.Failed"]);
	     } else{
	    	 showMessge(messageContent["Update.Error"]);
	     }
	     search();
	}
}
function sure(data){
    var result = data && data.result;
    if (result == "Success") {
		showMessge(messageContent["Delete.Success"]);
	} else if (result == "NotFound") {
		showMessge(messageContent["Delete.NotFound"]);
	} else {
		showMessge(messageContent["Delete.Error"]);
	}
    search();
}